﻿<?php
/**
 * This is the file to define DashboardOverview.
 * 
 * @author	Justin Yoo
 * @version	0.0.1.0001
 * @package	KYR.Components
 * 
 */

//	Checks if this file is called within the framework; otherwise terminates.
if (!defined("IS_FRAMEWORK") || !IS_FRAMEWORK)	header("Location: http://".$_SERVER["HTTP_HOST"]."/error/404");

//	Imports relevant files.
require_once CORE_PATH."KYR.Core.BaseComponent.php";

/**
 * This is class DashboardOverview that comprehensively overviews controls within the dashboard.
 *
 * Comprehensively overviews controls within the dashboard.
 * 
 * @author	Justin Yoo
 * @package	KYR.Components
 * 
 */
class DashboardOverview extends BaseComponent
{
	/**
	 * Initiates the DashboardOverview class.
	 *
	 * @access	public
	 * @param	Request	$request	Request object that contains page request details.
	 * @param	int		$objectID	Either moduleID or widgetID that calls the component.
	 * @param	string	$mappedBy	Flag whether the component is called by a module or widget.
	 * @param	int		$statusCode	Page status code.
	 * @exception					Throws an Exception while initialising the class.
	 *
	 */
	public function __construct($request, $objectID, $mappedBy, $statusCode = 200)
	{
		parent::__construct($request, $objectID, $mappedBy, $statusCode);
		$this->componentName = __CLASS__;

		if ($this->statusCode == 200)
		{
			try
			{
				$this->CheckComponent_();
				if (!$this->existComponent)
					throw new Exception("Component not found");
			}
			catch (Exception $ex) { throw $ex; }
		}
		else
			$this->existComponent = true;
		//	Creates controls for this component.
		$this->CreateControls_(); 
	}
	
	/**
	 * Creates a set of controls to be used in templates called by the component.
	 *
	 * @access	protected
	 *
	 */
	protected function CreateControls_()
	{
		//	Create controls for this component.
		$this->controls_ = array(
			"title"			=> array("type" => "text",	"value" => null,	"maxlength" => null,	"validation" => null),
			"contents"		=> array("type" => "array",	"value" => null,	"maxlength" => null,	"validation" => null)
			);
		//	Defines the content as a sub control for $this->controls_["contents"]["value"].
		$content = array(
			"contentID"		=> array("type" => "text",	"value" => null,	"maxlength" => null,	"validation" => null),
			"title"			=> array("type" => "text",	"value" => null,	"maxlength" => null,	"validation" => null),
			"uri"			=> array("type" => "text",	"value" => null,	"maxlength" => null,	"validation" => null),
			"content"		=> array("type" => "text",	"value" => null,	"maxlength" => null,	"validation" => null),
			"dateCreated"	=> array("type" => "text",	"value" => null,	"maxlength" => null,	"validation" => null)
			);
	}
	
	/**
	 * Processes the component.
	 *
	 * @access	public
	 * @param	array	$variables	Set of data to be stored by the component.
	 * @exception					Throws an Exception while processing the component.
	 *
	 */
	public function ProcessComponent(&$variables)
	{
		//	Processes get/post.
		$this->ProcessRequest($variables);
		
		//	Sets the controls.
		$this->SetControls_($variables);
		//Debug::PrintScreen("pre", $variables);

		//	Processes the parent method.
		try { $this->ProcessComponent_($variables); } catch (Exception $ex) { throw $ex; }

		//	Processes handlers
		if (isset($this->handlers) && count($this->handlers) > 0)
			foreach ($this->handlers as $handler)
				$handler->ProcessHandler($variables);
	}

	/**
	 * Processes the requests within the page.
	 *
	 * @access	public
	 * @param	array	$variables	The set of GET/POST variables.
	 * @exception		$ex			Throws an exception while processing the request.
	 *
	 */
	public function ProcessRequest(&$variables)
	{
		try
		{
			if ($this->request->isAjax)
				$this->ProcessAJAX($variables);
			else
				$this->ProcessGetPost($variables);
		}
		catch (Exception $ex) { throw $ex; }
	}
	
	/**
	 * Processes the AJAX requests within the page.
	 *
	 * @access	public
	 * @param	array	$variables	The set of GET/POST variables.
	 * @exception		$ex			Throws an exception while processing the request.
	 *
	 */
	public function ProcessAJAX(&$variables)
	{
		try
		{
		}
		catch (Exception $ex) { throw $ex; }
	}
	
	/**
	 * Processes the GET/POST requests within the page.
	 *
	 * @access	public
	 * @param	array	$variables	The set of GET/POST variables.
	 * @exception		$ex			Throws an exception while processing the request.
	 *
	 */
	public function ProcessGetPost(&$variables)
	{
		try
		{
		}
		catch (Exception $ex) { throw $ex; }
	}

	/**
	 * Sets the set of controls to be used in templates called by the component.
	 *
	 * @access	protected
	 * @param	array	$variables	Set of data generated by the component.
	 * @exception					Throws an Exception while setting data onto the controls.
	 *
	 */
	protected function SetControls_(&$variables)
	{
		//	Gets the list of contents.
		try { $contents = $this->_GetContents(); }
		catch (Exception $ex) { throw $ex; }
		//Debug::PrintScreen("pre", $contents);

		//	Sets the list of contents to the controls.
		$this->controls_["title"]["value"] = "List of Contents";
		foreach ($contents as $content)
			$this->controls_["contents"]["value"][]	= array(
				"contentID"		=> array("type" => "text",	"value" => $content["contentID"],	"maxlength" => null, "validation" => null),
				"title"			=> array("type" => "text",	"value" => $content["title"],		"maxlength" => null, "validation" => null),
				"uri"			=> array("type" => "text",	"value" => BASE_URL.$content["uri"],"maxlength" => null, "validation" => null),
				"content"		=> array("type" => "text",	"value" => $content["content"],		"maxlength" => null, "validation" => null),
				"dateCreated"	=> array("type" => "text",	"value" => date_format(date_create($content["dateCreated"]), "d/m/Y H:i:s"), "maxlength" => null, "validation" => null)
				);
		//Debug::PrintScreen("pre", $this->controls_);

		//	Adds the controls for this component.
		//Debug::PrintScreen("h1", $this->request->actionStep);
		switch ($this->request->actionStep)
		{
			case "view":
				$variables["controls"]["DashboardOverview"] = $this->controls_;
				break;
		}
	}
	
	/**
	* Gets the list of contents.
	*
	* @access	private
	* @return	array		Returns the list of contents.
	* @exception			Throws an Exception while retrieving data from the database.
	*
	*/
	private function _GetContents()
	{
		try
		{
			$db	= null;
			$contents = null;
			if ($this->statusCode == 200)
			{
				$db = new Database("MySQL");
				$qry = "CALL cms_GetContents()";
				$db->Query($qry);

				while ($content = $db->Fetch())
					$contents[] = $content;

				return $contents;
			}
		}
		catch (Exeption $ex) { throw $ex; }
	}
}
?>
